package com.pro.addresslist.dal.bll;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.pro.addresslist.dal.model.Relationship;
import com.pro.addresslist.tools.EncryptInterface;
import com.pro.addresslist.tools.EncryptRequest;
import com.pro.addresslist.tools.JSONHelper;
import com.pro.addresslist.tools.URLUtil;
/**
 * 
 * @author leekh
 *
 */
public class RelationshipBLL {
	
	/**
	 * 查询关系
	 * @param mContext
	 * @param parentID
	 */
	public static void SelectShip(Context mContext,String parentID){
		final List<Relationship> list=new ArrayList<Relationship>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectShip");
		mMap.put("parentID", parentID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "SelectShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 Relationship[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, Relationship.class);
					 for (Relationship relationship : uu) {
						 list.add(relationship);
				        	Log.i("bt","SelectShip+value==>"+relationship.get_Name()+">"+relationship.get_RelationshipID());
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("SelectShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectShip+err==>"+error.getMessage());


			}
		});
	}
	/**
	 * 添加关系
	 * @param mContext
	 * @param relationship
	 */
	public static void AddShip(Context mContext,Relationship relationship){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "AddShip");
		mMap.put("relationship", JSONHelper.toJSON(relationship));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "AddShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String stResult = result.optString("result");
				 if(stResult.length() != 0){
			        	Log.i("bt","AddShip+value==>"+stResult);

					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putString("result", stResult);//返回成功或失败succeed/failed
		             intent.putExtras(bundle);
		             intent.setAction("AddShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","AddShip+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 修改关系（相对应联系人的关系也要修改）
	 * @param mContext
	 * @param relationship
	 */
	public static void AlterShip(Context mContext,Relationship relationship){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "AlterShip");
		mMap.put("relationship", JSONHelper.toJSON(relationship));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "AlterShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String stResult = result.optString("result");
				 if(stResult.length() != 0){
			        	Log.i("bt","AlterShip+value==>"+stResult);
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putString("result", stResult);//返回成功或失败succeed/failed
		             intent.putExtras(bundle);
		             intent.setAction("AlterShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","AlterShip+err==>"+error.getMessage());

			}
		});
	}
	
	/**
	 * 删除关系（相对应联系人的关系也要修改）
	 * @param mContext
	 * @param relationshipID
	 */
	public static void DeleteShip(Context mContext,String relationshipID){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "DeleteShip");
		mMap.put("relationshipID", relationshipID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "DeleteShip", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String stResult = result.optString("result");
				 if(stResult.length() != 0){
			        	Log.i("bt","DeleteShip+value==>"+stResult);

					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putString("result", stResult);//返回成功或失败succeed/failed
		             intent.putExtras(bundle);
		             intent.setAction("DeleteShip");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","DeleteShip+err==>"+error.getMessage());

			}
		});
	}
	
	
}
